
import ProForm, { ProFormText, ProFormTextArea, ProFormCascader } from '@ant-design/pro-form';
import { useUpdateEffect } from '@umijs/hooks';
import { Drawer } from 'antd';
import { Spin } from 'antd';
import { getCascader } from "../service";
import { useRequestHandle } from '@/utils/utils';
import { useState } from 'react';
import { useRef } from 'react';

const SaveForm = (props) => {
  const { modalVisible, onCancel, onSubmit, loading, record } = props;
  const [agentList, setAgentList] = useState([]);
  const formRef = useRef();
  const getCascaderRequest = useRequestHandle(getCascader, (data) => {
    // setBackupPath(response.data);
    setAgentList(data)
  });
  useUpdateEffect(() => {
    if (modalVisible) {
      // getAgentRootPathRequest.run({ id })
      getCascaderRequest.run();
    }
  }, [modalVisible])
  useUpdateEffect(() => {
    // if (record.id) {

    // }
  }, [record])


  return (
    <Drawer
      destroyOnClose
      title={'发布'}
      open={modalVisible}
      // onCancel={() => onCancel()}
      onClose={() => onCancel()}
      width={600}
      footer={null}
    >
      <Spin tip="正在处理..." spinning={loading}>
        <ProForm
          onFinish={async (value) => {
            const params = value;

            params.agentServerId = params.serverIds[0];
            params.agentServerConfigId = params.serverIds[1];
            await onSubmit(params);
          }}

          formRef={formRef}
          initialValues={{ ...record }}
        >
          <ProFormCascader
            name="serverIds"
            label="服务"
            rules={[{ required: true, message: '请选择服务' }]}
            fieldProps={{
              options:agentList
            }}
          />

          <ProForm.Group>
            <ProFormText
              name="name"
              label="版本名称"
              fieldProps={{
                allowClear: true,
              }}
              placeholder="请输入版本名称"
              rules={[{ required: true, message: '请输入版本名称！' }]}
            />

          </ProForm.Group>

          <ProFormTextArea
            name="context"
            label="版本内容"
            fieldProps={{
              allowClear: true,
            }}
            placeholder="请输入版本内容"
            rules={[{ required: false, message: '请输入版本内容' }]} />


        </ProForm>
      </Spin>
    </Drawer>
  );
};

export default SaveForm;
